# Environment Check Script
# Checks required software versions for development

$ErrorActionPreference = "Stop"
$reportFile = "environment_report.txt"

function Test-Cmd {
    param($cmd, $name)
    try {
        $result = & $cmd --version 2>&1
        if ($LASTEXITCODE -eq 0) {
            Write-Host "PASS: $name installed - $($result.Trim())"
            return $result.Trim()
        } else {
            Write-Host "FAIL: $name not working properly"
            return $null
        }
    } catch {
        Write-Host "FAIL: $name not installed - $($_.Exception.Message)"
        return $null
    }
}

function Test-Environment {
    Write-Host "=" * 50
    Write-Host "ENVIRONMENT CHECK"
    Write-Host "=" * 50
    
    $results = @{}
    
    # System info
    Write-Host "`nSYSTEM INFORMATION:"
    $results.OS = [System.Environment]::OSVersion.VersionString
    $results.PowerShell = $PSVersionTable.PSVersion.ToString()
    Write-Host "OS: $($results.OS)"
    Write-Host "PowerShell: $($results.PowerShell)"
    
    # Python
    Write-Host "`nPYTHON:"
    $results.Python = Test-Cmd "python" "Python"
    $results.Python3 = Test-Cmd "python3" "Python 3"
    $results.Pip = Test-Cmd "pip" "pip"
    $results.Pip3 = Test-Cmd "pip3" "pip3"
    
    # Node.js
    Write-Host "`nNODE.JS:"
    $results.NodeJS = Test-Cmd "node" "Node.js"
    $results.NPM = Test-Cmd "npm" "npm"
    
    # Git
    Write-Host "`nGIT:"
    $results.Git = Test-Cmd "git" "Git"
    
    # Database tools
    Write-Host "`nDATABASE TOOLS:"
    try {
        $sqlcmd = Get-Command "sqlcmd" -ErrorAction SilentlyContinue
        if ($sqlcmd) {
            $results.SQLCmd = "Installed"
            Write-Host "PASS: sqlcmd installed"
        } else {
            $results.SQLCmd = "Not installed"
            Write-Host "FAIL: sqlcmd not installed"
        }
    } catch {
        $results.SQLCmd = "Check failed"
        Write-Host "FAIL: sqlcmd check failed"
    }
    
    return $results
}

function Write-Report {
    param($results)
    
    $report = @"
ENVIRONMENT CHECK REPORT
Generated: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')

SYSTEM INFORMATION:
- OS: $($results.OS)
- PowerShell: $($results.PowerShell)

PYTHON:
- Python: $($results.Python)
- Python 3: $($results.Python3)
- pip: $($results.Pip)
- pip3: $($results.Pip3)

NODE.JS:
- Node.js: $($results.NodeJS)
- npm: $($results.NPM)

GIT:
- Git: $($results.Git)

DATABASE TOOLS:
- sqlcmd: $($results.SQLCmd)

ENVIRONMENT STATUS:
$(
if (-not $results.Python -and -not $results.Python3) {
    "FAIL: Missing Python environment - Install Python 3.10+"
} else {
    "PASS: Python environment OK"
}
)

$(
if (-not $results.NodeJS) {
    "FAIL: Missing Node.js environment - Install Node.js 16+"
} else {
    "PASS: Node.js environment OK"
}
)

RECOMMENDED ACTIONS:
1. Install Python 3.10+ if missing
2. Install Node.js 16+ if missing  
3. Install database drivers
4. Configure environment variables
"@
    
    $report | Out-File -FilePath $reportFile -Encoding UTF8
    Write-Host "`nREPORT SAVED TO: $reportFile"
}

# Main execution
try {
    $results = Test-Environment
    Write-Report $results
    
    # Check critical dependencies
    $missing = @()
    if (-not $results.Python -and -not $results.Python3) { $missing += "Python" }
    if (-not $results.NodeJS) { $missing += "Node.js" }
    
    if ($missing.Count -gt 0) {
        Write-Host "`nFAIL: Missing critical dependencies - $($missing -join ', ')" -ForegroundColor Red
        exit 1
    } else {
        Write-Host "`nPASS: Environment check completed successfully" -ForegroundColor Green
        exit 0
    }
} catch {
    Write-Host "FAIL: Environment check failed - $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}